package com.concerneyes.mine.TabActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.mine.Task.MyInfoImpl;
import com.concerneyes.mine.Task.MyInfoListener;
import com.concerneyes.mine.model.MyInfoModel;

import common.AbsActivity;
import util.ToastHelper;

/**
 * 个人资料信息二级修改页面
 */

public class EditInfoActivity extends AbsActivity implements View.OnClickListener, MyInfoListener {
    private EditText mEtChange;
    private String title;
    /**
     * 发送验证码
     */
    private TextView mTvSendCode;
    private EditText mEtCode;
    private LinearLayout mLlEditCode;
    /**
     * 绑定手机号有助于提高账号安全指数
     */
    private TextView mTvBindInfo;
    /**
     * 完  成
     */
    private TextView mTvDone;
    private MyInfoModel model;
    private MyInfoImpl impl;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_edit_info);
        title = getIntent().getExtras().getString("title");
        model = (MyInfoModel) getIntent().getExtras().getSerializable("MyInfoModel");
        if (title == null) {
            return;
        }
        setTitle(title);
        getBackButton().setVisibility(View.GONE);
        getLeftText().setVisibility(View.VISIBLE);
        getRightText().setVisibility(View.VISIBLE);
        getLeftText().setText(R.string.cancel);
        getRightText().setText(R.string.save);
        impl = new MyInfoImpl(this);
        initView();
    }

    @Override
    public void onRightTextAction(View v) {
        super.onRightTextAction(v);
        if (title.equals(getResources().getString(R.string.my_name))) {
            model.setNickname(editText());
        } else if (title.equals(getResources().getString(R.string.my_zhiye))) {
            model.setJob(editText());
        }
        impl.myInfoUpdateImpl(model);
    }

    private String editText() {
        String text = mEtChange.getText().toString();
        if (TextUtils.isEmpty(text)) {
            ToastHelper.getInstance()._toast(String.format(getResources().getString(R.string.place_edit_), title));
            return null;
        }
        return text;
    }

    @Override
    public void onLeftTextAction(View v) {
        super.onLeftTextAction(v);
        finish();
    }

    @Override
    public void onRightAction(View v) {
        super.onRightAction(v);
    }

    private void initView() {
        mEtChange = (EditText) findViewById(R.id.et_change);
        mEtChange.setHint(String.format(getResources().getString(R.string.place_edit_), title));
        mTvSendCode = (TextView) findViewById(R.id.tv_send_code);
        mEtCode = (EditText) findViewById(R.id.et_code);
        mLlEditCode = (LinearLayout) findViewById(R.id.ll_edit_code);
        mTvBindInfo = (TextView) findViewById(R.id.tv_bind_info);
        mTvDone = (TextView) findViewById(R.id.tv_done);
        if (title.equals(getResources().getString(R.string.my_phone))) {
            getRightText().setVisibility(View.GONE);
            mTvSendCode.setVisibility(View.VISIBLE);
            mLlEditCode.setVisibility(View.VISIBLE);
            mTvDone.setVisibility(View.VISIBLE);
            mTvBindInfo.setVisibility(View.VISIBLE);
            mTvSendCode.setVisibility(View.VISIBLE);
            mTvSendCode.setOnClickListener(this);
            mTvDone.setOnClickListener(this);
            mEtCode.setHint(R.string.edit_code);
        }
        setData();
    }

    private void setData() {
        if (title.equals(getResources().getString(R.string.my_name))) {
            if (!TextUtils.isEmpty(model.getNickname())) {
                mEtChange.setText(model.getNickname());
            }
        } else if (title.equals(getResources().getString(R.string.my_zhiye))) {
            if (!TextUtils.isEmpty(model.getJob())) {
                mEtChange.setText(model.getJob());
            }
        }
        model.getNickname();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_send_code:
                break;
            case R.id.tv_done:
                break;
        }
    }

    @Override
    public void onMyInfoSuccess(MyInfoModel data) {

    }

    @Override
    public void onMyInfoFail() {

    }

    @Override
    public void onMyInfoUpdateSuccess(MyInfoModel model) {
        finish();
    }

    @Override
    public void onMyInfoUpdateFail() {

    }
}
